 /**
  ********************************************
  Node Class
  ********************************************
  
 * This program uses "Node" in conjucation with "LinkedList" to complete Homework 3
 * 
 * This class comes with a "remove" method on top of what was already here
 * 
 * @author (Greg Shevach & Chris Nerf) 
 * @version (v1.1)
 * @date (12/09/11)
 * 
 * Added a way to ensure that multiple, non-uniquely identifiable datas will only use one node.
 * for future sorting.
 */

public class Node<T> {

    private T data;
    private Node<T> nextLeft;
    private Node<T> nextRight;
    private String huffmanCode = "";
    private int count = 0; //added this to keep track of the number of this type of element

    public Node() {
	data = null;
	nextLeft = null;
	nextRight = null;
    }

    public Node( T element ) {
	data = element;
	nextLeft = null;
	count += 1; //increase the count on adding a new element
    }

    public Node<T> getnextLeft() {
	return ( nextLeft );
    }
    
    public Node<T> getnextRight() {
    	return ( nextRight );
        }

    public void setnextLeft( Node<T> node ) {
	nextLeft = node;
    }
    
    public void setnextRight (Node<T> node ) {
    	nextRight = node;
        }

    public T getElement() {
	return ( data );
    }

    public void setElement( T element ) {
	data = element;
    }

    public String toString() {
	String s = new String();

	s += "data: " + data.toString();
	s += "nextLeft: " + nextLeft;

	return ( s );
    }
    
    public void remove(T element) {  //Sets the element equal to null
    data = element;
    data = null;
  }
    
    public int getCount() //gotta get the count
    {
    	return this.count;
    }
    
    public void increaseCount() //gotta increase the count if the element already exists
    {
    	this.count++;
    }
    
    public void printValue()
    {
    	System.out.print(this.data);
    	System.out.print(": " + this.count);
    }
    
    //get the value of the entire branch
    public int getBranchProbability()
    {
    	if (this.getnextLeft() == null && this.getnextRight() == null)
    		return this.count;
    	else
    	{
    		return this.getnextLeft().getBranchProbability() + this.getnextRight().getBranchProbability();
    	}
    }
    
    //need a way to set the current count, makes sorting easier
    public void setCount(int count)
    {
    	
    	this.count = count;
    	
    }
    
    public boolean isLeaf()
    {
    	if(this.getnextLeft() == null && this.getnextRight()==null)
    		return true;
    	return false;
    }

    public void setHoffmanCode(String huffmanCode)
    {
    	this.huffmanCode = huffmanCode;
    }
    
}

